package org.example.shop;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CustomerMenu {

        HashMap<String, Customer> customerMap = new HashMap<>();           //用于存放客户信息
        HashMap<String, Customer> IDMap = new HashMap<>();                 //用于存放客户信息
        List<String> purchaseHistory = new ArrayList<>();
        Map<String, String> history = new HashMap<>();

        Random random = new Random();
        Scanner sc = new Scanner(System.in);
        Car car = new Car();

        public int customerMenu(Customer c2, ManagerMenu me) {
            System.out.println("请选择：1.注册 2.登录");
            int choose = sc.nextInt();
            if (choose == 1) {            //注册
                System.out.println("请输入用户名（不少于5个字符）");
                do {     String name = sc.next();
                    if (name.length() >= 5) {
                        c2.setCustomerName(name);
                        break;
                    } else {
                        System.out.println("输入错误，请再次输入用户名");
                    }
                } while (true);                      //用户名输入完毕

                System.out.println("请输入密码（密码长度大于8个字符，必须是大小写字母、数字和标点符号的组合）");
                do {
                    String code = sc.next();
                    if (code.length() > 8 && contain(code)) {
                        c2.setCustomerCode(code);
                        break;
                    } else {
                        System.out.println("输入错误，请再次输入密码");
                    }
                } while (true);                    //密码输入完毕 注册成功
                System.out.println("注册成功");
                customerMap.put(c2.getCustomerName(), c2);   //保存
            } else if (choose == 2) {              //登录
                int n = 0;
                do {
                    System.out.println("请输入用户名");
                    String name = sc.next();
                    if (customerMap.get(name) == null) {
                        System.out.println("用户名不存在");
                    } else {do {
                        System.out.println("请输入密码");
                        String code = sc.next();
                        if (customerMap.get(name).getCustomerCode().equals(code)) {
                            System.out.println("登录成功");
                            break;
                        } else {
                            System.out.println("密码不正确");
                            n++;
                        }
                    } while (n < 5);
                        break;
                    }
                } while (true);
                if (n == 5) {
                    System.out.println("账户已锁定");
                    return 0;
                }
            } else {
                System.out.println("输入错误");
                return 0;
            }                                        //登录完毕  进入菜单
            boolean flag = true;
            do {
                System.out.println("菜单：1.修改自身密码 2.忘记密码 3.商品加入购物车 4.商品从购物车中移除");
                System.out.println("     5.修改购物车中的商品 6.结账 7.查看购物历史 8.退出");
                int choose2 = sc.nextInt();  if (choose2 == 1) {                                                  //修改自身密码
                    changeMyCode();
                } else if (choose2 == 2) {                                          //忘记密码
                    forgetCode();
                } else if (choose2 == 3) {
                    putShoppingCar(me);
                } else if (choose2 == 4) {
                    delShoppingCar();
                } else if (choose2 == 5) {
                    changeShoppingCar();
                } else if (choose2 == 6) {
                    pay();
                } else if (choose2 == 7) {
                    searchHistory();
                } else if (choose2 == 8) {                                                //退出
                    System.out.println("退出");
                    flag = false;
                } else {
                    System.out.println("输入错误");
                }
            } while (flag);
            return 0;
        }

        public void changeMyCode() {
            System.out.println("请输入用户名和原密码");
            String name = sc.next(); String oldCode = sc.next();
            if (customerMap.get(name) == null) {
                System.out.println("用户名不存在");
            } else {
                if (customerMap.get(name).getCustomerCode().equals(oldCode)) {
                    System.out.println("请输入新密码");
                    String newCode = sc.next();
                    if (newCode.length() > 8 && contain(newCode)) {
                        customerMap.get(name).setCustomerCode(newCode);
                        System.out.println("修改成功");
                    } else {
                        System.out.println("新密码不符合要求");
                    }

                } else {
                    System.out.println("用户名或原密码错误，无法修改");
                }
            }
        }

        public void forgetCode() {
            System.out.println("请输入用户名和注册所使用的邮箱地址");
            String name = sc.next();
            String customerEmail = sc.next();
            if (customerMap.containsKey(name) && customerMap.get(name).getCustomerEmail().equals(customerEmail)) {
                String newCode = randomString();
                System.out.println(newCode);
                System.out.println("系统会将一个随机生成的密码发至邮箱，可以使用该密码登录");
            } else {
                System.out.println("用户名或邮箱地址错误");
            }
        }


        public void putShoppingCar(ManagerMenu me) {
            System.out.println("请输入商品名称和数量");
            String name = sc.next();
            int quantity = sc.nextInt();
            if (me.goodsMap.get(name) == null) {
                System.out.println("该商品不存在");
            } else {
                Goods g = me.goodsMap.get(name);
                car.addItem(g, quantity);
                System.out.println("添加成功");
            }
        }

        public void delShoppingCar() {
            System.out.println("请输入要移除的商品编号");
            String productId = sc.next();
            System.out.println("请确认：1.删除 2.取消");
            int select = sc.nextInt();
            if (select == 1) {
                car.removeItem(productId);
                System.out.println("移除成功");
            }
        } public void changeShoppingCar() {
            System.out.println("请输入要修改的商品编号和修改数量");
            String productId = sc.next();
            int quantity = sc.nextInt();
            car.updateItemQuantity(productId, quantity);
            System.out.println("修改成功");
        }

        public void pay() {
            System.out.println("请输入支付方式");
            String payMethod = sc.next();
            double total = car.checkout(payMethod);
            purchaseHistory.add("Purchased items for " + total + " using " + payMethod);
            history.put(String.valueOf(System.currentTimeMillis()), car.toString());
        }

        public void searchHistory() {

        }

        public boolean contain(String str) {
            String regex = "(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[\\p{Punct}])";   // 正则表达式匹配：包含大小写字母、数字和标点符号
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(str);
            return matcher.find();
        }
        public String randomString() {
            int length = random.nextInt(4) + 9;
            String upperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";              // 定义字符集合
            String lowerLetters = "abcdefghijklmnopqrstuvwxyz";
            String digits = "0123456789";
            String punctuation = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";

            StringBuilder randomString = new StringBuilder();                    // 使用 StringBuilder 构建结果字符串

            randomString.append(upperLetters.charAt(random.nextInt(upperLetters.length())));  //至少包含一个大写字母、一个小写字母
            randomString.append(lowerLetters.charAt(random.nextInt(lowerLetters.length())));  //一个数字和一个标点符号
            randomString.append(digits.charAt(random.nextInt(digits.length())));
            randomString.append(punctuation.charAt(random.nextInt(punctuation.length())));

            for (int i = 4; i < length; i++) {                                          // 生成剩余部分的随机字符
                String charSet = upperLetters + lowerLetters + digits + punctuation;    // 随机选择字符集合中的一个字符
                randomString.append(charSet.charAt(random.nextInt(charSet.length())));
            }
            return randomString.toString();
        }
    }
